package cn.itcast.bos.web.action.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.service.base.CourierService;
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class CourierAction extends ActionSupport implements ModelDriven<Courier>{
	private Courier courier = new Courier();
	@Override
	public Courier getModel() {
		return courier;
	}
	
	@Autowired
	private CourierService courierService;
	
	//属性驱动
	private Integer page;
	private Integer rows;

	public void setPage(Integer page) {
		this.page = page;
	}


	public void setRows(Integer rows) {
		this.rows = rows;
	}


	@Action(value="courier_save",results={@Result(name="success",location="./pages/base/courier.html",type="redirect")})
	public String save(){
		System.out.println("保存操作开始执行。。。。。。。。。。。。。。");
		courierService.save(courier);
		return SUCCESS;
	}
	
	@SuppressWarnings("all") 
	@Action(value="courier_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		System.out.println("这分页查询。。。。。。。。。。。。。。。。。。。。。。");
		Pageable pageable = new PageRequest(page-1, rows);
		Specification<Courier> spcifiction = new Specification(){

			@Override
			public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList();
				
				//根据工号查询
				if(StringUtils.isNotBlank(courier.getCourierNum())){
					Predicate p1 = cb.equal(root.get("courierNum").as(String.class), courier.getCourierNum());
					list.add(p1);
				}
				
				//根据所属单位模糊查询
				if(StringUtils.isNotBlank(courier.getCompany())){
					Predicate p2 = cb.like(root.get("company").as(String.class), "%"+courier.getCompany()+"%");
					list.add(p2);
				}
				
				//根据快递型号等值查询
				if(StringUtils.isNotBlank(courier.getType())){
					Predicate p3 = cb.equal(root.get("type"), courier.getType());
					list.add(p3);
				}
				//多表关联查询
				Join<Object,Object> standardRoot = root.join("standard",JoinType.INNER);
				if(courier.getStandard() != null && StringUtils.isNotBlank(courier.getStandard().getName())){
					Predicate p4 = cb.like(standardRoot.get("name").as(String.class), "%"+courier.getStandard().getName()+"%");
					list.add(p4);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}};
			Page<Courier> pageData = courierService.findAll(spcifiction,pageable);//调用业务层
			
			
			Map<String,Object> result = new HashMap<String,Object>();
			result.put("total",pageData.getTotalElements());
			result.put("rows", pageData.getContent());
			ActionContext.getContext().getValueStack().push(result);;
			return "success";
	}
	
	//属性驱动
	private String ids;
	
	
	public void setIds(String ids) {
		this.ids = ids;
	}


	//批量删除快递员
	@Action(value="courier_delBatch",results={@Result(name="success",location="./pages/base/courier.html",type="redirect")})
	public String delBatch(){
		String[] idArray = ids.split(",");
		courierService.delBatch(idArray);
		return "success";
	}
	
	//查询所有未关联定区的快递员
	@Action(value="courier_findnoassociation",results={@Result(name="success",type="json")})
	public String findnoassociation(){
		List<Courier> courier = courierService.findnoassociation();
		ActionContext.getContext().getValueStack().push(courier);
		return SUCCESS;
	}
}
